// 复制到剪贴板
export const copyTextToClipboard = function(text) {
    var textArea = document.createElement('textarea')

    textArea.style.position = 'fixed'
    textArea.style.top = 0
    textArea.style.left = 0
    textArea.style.width = '2em'
    textArea.style.height = '2em'
    textArea.style.padding = 0
    textArea.style.border = 'none'
    textArea.style.outline = 'none'
    textArea.style.boxShadow = 'none'
    textArea.style.background = 'transparent'
    textArea.value = text
    document.body.appendChild(textArea)
    textArea.select()
    try {
        var successful = document.execCommand('copy')
        var msg = successful ? 'successful' : 'unsuccessful'
            // console.log('Copying text command was ' + msg)
    } catch (err) {
        // console.log('Oops, unable to copy')
    }
    document.body.removeChild(textArea)
}

// 格式化时间
export const formatTime = function(time, status) {
    var t = time
    var commonTime = new Date(t)
    var y =
        commonTime.getFullYear() >= 10 ?
        commonTime.getFullYear() :
        '0' + commonTime.getFullYear()
    var month =
        commonTime.getMonth() + 1 >= 10 ?
        commonTime.getMonth() + 1 :
        '0' + (commonTime.getMonth() + 1)
    var d =
        commonTime.getDate() >= 10 ?
        commonTime.getDate() :
        '0' + commonTime.getDate()
    var h =
        commonTime.getHours() >= 10 ?
        commonTime.getHours() :
        '0' + commonTime.getHours()
    var m =
        commonTime.getMinutes() >= 10 ?
        commonTime.getMinutes() :
        '0' + commonTime.getMinutes()
    var s =
        commonTime.getSeconds() >= 10 ?
        commonTime.getSeconds() :
        '0' + commonTime.getSeconds()
    if (status === 1) {
        return h + ':' + m + ':' + s
    } else if (status === 2) {
        return y + '-' + month + '-' + d + ' ' + h + ':' + m
    } else if (status === 3) {
        return y + '-' + month + '-' + d + ' ' + h + ':' + m + ':' + s
    } else if (status === 4) {
        return y + '-' + month + '-' + d
    }
}

// 上传
export const upFiles = function(event, success) { //压缩图片后上传
    var eleFile = event.target.files[0];
    var reader = new FileReader(),
        img = new Image();

    // 选择的文件对象
    var file = null;
    file = event.target.files[0];
    // 选择的文件是图片
    if (file.type.indexOf("image") == 0) {
        reader.readAsDataURL(file);
    }

    // 文件base64化，以便获知图片原始尺寸
    reader.onload = function(e) {
        img.src = e.target.result;
    };

    // 缩放图片需要的canvas
    var canvas = document.createElement('canvas');
    var context = canvas.getContext('2d');

    img.onload = function() {

        var originWidth = this.width;
        var originHeight = this.height;

        // 最大尺寸限制
        var maxWidth = 400,
            maxHeight = 400;

        // 目标尺寸
        var targetWidth = originWidth,
            targetHeight = originHeight;

        // 图片尺寸超过最大尺寸的限制
        if (originWidth > maxWidth || originHeight > maxHeight) {
            if (originWidth / originHeight > maxWidth / maxHeight) {
                // 更宽，按照宽度限定尺寸
                targetWidth = maxWidth;
                targetHeight = Math.round(maxWidth * (originHeight / originWidth));
            } else {
                targetHeight = maxHeight;
                targetWidth = Math.round(maxHeight * (originWidth / originHeight));
            }
        }

        // canvas对图片进行缩放
        canvas.width = targetWidth;
        canvas.height = targetHeight;

        // 清除画布
        context.clearRect(0, 0, targetWidth, targetHeight);

        // 图片压缩
        context.drawImage(img, 0, 0, targetWidth, targetHeight);

        //利用Blob插件转换，成数据
        canvas.toBlob(function(blob) {

            var form = new FormData();
            // form.append('file', blob);
            form.append("fileName", "123jpg")

            success(form);
        });
    }
}

// pop显示全部文字
export const propTip = () => {
        var tar = document.getElementsByName('pop');
        var tt = document.getElementsByClassName('dis-tip-s');
        for (var i = 0; i < tar.length; i++) {
            let startX, startY;
            (function(i) {
                tar[i].addEventListener('mouseover', function(event) {
                    event.preventDefault();
                    for (var k = 0; k < tt.length; k++) {
                        tt[k].parentNode.removeChild(tt[k])
                    }
                    startX = event.pageX; //触碰开始时的X坐标
                    startY = getComputedStyle(tar[i], null)['height'].replace('px', '') - 10; //触碰开始时的X坐标
                    let dom = document.createElement('div');
                    dom.className = 'dis-tip-s';
                    dom.innerText = this.getAttribute('pops');
                    dom.style.left = '15%';
                    dom.style.top = startY + 'px';
                    tar[i].appendChild(dom);
                }, false)
                tar[i].addEventListener('mouseleave', function(event) {
                    event.preventDefault();
                    for (var k = 0; k < tt.length; k++) {
                        tt[k].parentNode.removeChild(tt[k])
                    }
                }, false)
            })(i)
        }
    }
    //显示tip
export const createdPropTip = () => {
    var tar = document.getElementsByName('pop');
    var tt = document.getElementsByClassName('dis-tip-s');
    for (var i = 0; i < tar.length; i++) {
        let startX, startY;
        (function(i) {
            let con = document.createElement('span');
            con.className = 'dis-tip-con';
            con.style.width = 'auto';
            con.style['max-width'] = '90%';
            con.style.display = 'block';
            con.style.overflow = 'hidden';
            con.style['text-overflow'] = 'ellipsis';
            con.style['white-space'] = 'nowrap';
            con.style.position = 'relative';
            con.style['z-index'] = 9;
            // con.style['font-size'] = 0;
            con.innerText = tar[i].getAttribute('pops');
            tar[i].appendChild(con);

            con.addEventListener('mouseover', function(event) {
                event.preventDefault();
                for (var k = 0; k < tt.length; k++) {
                    tt[k].parentNode.removeChild(tt[k])
                }
                startX = event.pageX; //触碰开始时的X坐标
                startY = Number(getComputedStyle(tar[i], null)['height'].replace('px', '')); //触碰开始时的X坐标
                let DomWidth = con.offsetWidth;
                let dom = document.createElement('div');
                dom.className = 'dis-tip-s';
                dom.innerText = tar[i].getAttribute('pops');
                dom.style.margin = '0 auto';
                dom.style.top = startY + 'px';
                tar[i].appendChild(dom);

                let tip = document.createElement('div');
                tip.className = 'dis-tip-s-up';
                dom.appendChild(tip);
            }, false)
            con.addEventListener('mouseleave', function(event) {
                event.preventDefault();
                for (var k = 0; k < tt.length; k++) {
                    tt[k].parentNode.removeChild(tt[k])
                }
            }, false)
        })(i)
    }
}

export const buildExceptKeyAndValue = ((array, key, value, expectKey, expectValue) => {
    let resultArray = [];
    array.forEach(item => {
        resultArray.push({
            [expectKey]: item[key],
            [expectValue]: item[value],
            item: item
        })
    });
    return resultArray;
});


export const formatSystemDate = ((date) => {
    if (!date) {
        return '';
    }
    let dt = new Date(date).toJSON();
    return new Date(+new Date(dt) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')

});
